﻿using System;
using System.IO;
using System.Windows.Forms;
using GoogleEarthClasses.Global;
using GoogleEarthClasses.Entities;
using System.Collections.Generic;
namespace ThirdPartyLibraries
{
    public partial class SelectingDatabase : UserControl
    {
        protected string m_SelectedFolder = "";
        public string SelectedFolder
        {
            get { return m_SelectedFolder; }
        }
        public void showStep(Int32 Step)
        {
            switch (Step)
	        {
                case 1:
                    panelstep1.Visible = true;
                    panelstep2.Visible = false;
                    break;
                case 2:
                    panelstep1.Visible = false;
                    panelstep2.Visible = true;
                    break;
	        }
        }

        protected bool m_IsNew;
        public bool IsNew
        {
            get { return m_IsNew; }
        }

        public bool CopyMobileVersion
        {
            get { return checkBoxCopynew.Checked; }
        }

        public SelectingDatabase()
        {
            InitializeComponent();
        }

        private void SelectDatabase_Load(object sender, System.EventArgs e)
        {
            LoadListBox();
            if (radioButtonExisting.Checked)
                listBox1.Visible = true;
        }

        private void loadFileDialog()
        {
            m_IsNew = true;
            listBox1.Visible = false;
            switch (folderBrowserDialogNewMobile.ShowDialog())
            {
                case DialogResult.OK:
                    LocateDatabases _Locatedatabases = new LocateDatabases();
                    _Locatedatabases.SDFDatabasesPath = GetSdfPath(folderBrowserDialogNewMobile.SelectedPath);
                    if (_Locatedatabases.TestMobileConnection())
                    {
                        if (!_Locatedatabases.CheckExistingTable("tb_Config"))
                        {
                            _Locatedatabases.CreateConfigTable();
                        }
                        _Locatedatabases.UpdateTableSchema();
                        m_SelectedFolder = folderBrowserDialogNewMobile.SelectedPath;
                        labelSelectFolder.Text = folderBrowserDialogNewMobile.SelectedPath;
                        labelAmountImages.Text = _Locatedatabases.getAmountImages().ToString();
                        labelAmountPositions.Text = _Locatedatabases.getAmountPositions().ToString();
                        labelAmountTotalUploaded.Text = _Locatedatabases.getTotalUploaded().ToString();
                        showStep(2);
                    }
                    else
                        m_SelectedFolder = "";
                    break;
                default:
                    break;
            }
            checkBoxCopynew.Visible = m_IsNew;
        }

        private void LoadListBox()
        {
            if (!Directory.Exists(Constants.K_OutboxPath))
                Directory.CreateDirectory(Constants.K_OutboxPath);
            listBox1.Visible = true;
            DirectoryInfo _Dir = new DirectoryInfo(Constants.K_OutboxPath);
            DirectoryInfo[] _Dirs = _Dir.GetDirectories();
            Array.Sort(_Dirs, new DirectorySort());
            foreach (DirectoryInfo d in _Dirs)
            {
                if(!String.IsNullOrEmpty(GetSdfPath(d.FullName)))
                    listBox1.Items.Add(d);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            try
            {
                m_IsNew = false;
                ListBox _tmp_ListBox = (ListBox)sender;
                DirectoryInfo _tmb_SelectedItem = (DirectoryInfo)_tmp_ListBox.SelectedItem;
                m_SelectedFolder = _tmb_SelectedItem.FullName;
                checkBoxCopynew.Visible = m_IsNew;

                LocateDatabases m_LocateDabase = new LocateDatabases();
                m_LocateDabase.SDFDatabasesPath = GetSdfPath(_tmb_SelectedItem.FullName);
                labelSelectFolder.Text = _tmb_SelectedItem.FullName;
                labelAmountImages.Text = m_LocateDabase.getAmountImages().ToString();
                labelAmountPositions.Text = m_LocateDabase.getAmountPositions().ToString();
                labelAmountTotalUploaded.Text = m_LocateDabase.getTotalUploaded().ToString();
                showStep(2);
            }
            catch (Exception ex)
            {
                ErrorManager.CreateDebug("listBox1_SelectedIndexChanged->\n" + ex.Message + "\n" + ex.StackTrace);
            }

  
        }

        private void radioButtonExisting_CheckedChanged(object sender, System.EventArgs e)
        {
            RadioButton _tmpRadio = (RadioButton)sender;
            if (_tmpRadio.Checked)
                listBox1.Visible = true;
            
        }

        private void radioButtonMobileDatabase_CheckedChanged(object sender, System.EventArgs e)
        {
            RadioButton _tmpRadio = (RadioButton)sender;
            if (_tmpRadio.Checked)
                loadFileDialog();
        }

        protected string GetSdfPath(string Folder)
        {
            try
            {
                DirectoryInfo _d = new DirectoryInfo(Folder);
                FileInfo[] _Files = _d.GetFiles("*.sdf");
                if (_Files.Length > 0)
                    return _Files[0].FullName;
                return "";


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}
